﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Xml.Linq;
using UnityEditor;
using UnityEngine;

internal class PostProcessMetroPlayer
{
    private static readonly string[] dontOverwriteFiles = new string[] { "pch.cpp", "pch.h", "App.xaml", "App.xaml.cpp", "App.xaml.h", "MainPage.xaml", "MainPage.xaml.cpp", "MainPage.xaml.h", "Package.appxmanifest", @"Common\StandardStyles.xaml", @"Assets\SquareTile.png", @"Assets\SmallTile.png", @"Assets\StoreLogo.png", @"Assets\SplashScreen.png" };
    private const string manifestFile = "AppxManifest.xml";

    private static string CombinePath(string path1, string path2)
    {
        return Path.Combine(path1, path2).Replace('\\', '/');
    }

    private static List<string> CopyAssemblies(string srcDirectory, string targetDirectory, string extension)
    {
        List<string> list = new List<string>();
        foreach (string str in Directory.GetFiles(srcDirectory, "*." + extension))
        {
            string fileName = Path.GetFileName(str);
            if (extension == "winmd")
            {
                fileName = Path.GetFileNameWithoutExtension(fileName) + ".dll";
            }
            string path = Path.Combine(targetDirectory, fileName);
            if (!File.Exists(path))
            {
                File.Copy(str, path);
                list.Add(fileName);
            }
        }
        return list;
    }

    private static void CopyPlayerFiles(BuildTarget target, BuildOptions options, string stagingArea, string playerPackage)
    {
        if (EditorUserBuildSettings.metroBuildType == MetroBuildType.VisualStudio)
        {
            FileUtil.CopyDirectoryRecursive(playerPackage + "/Players", stagingArea + "/Players", true, true);
            return;
        }
        string[] textArray1 = new string[] { "Players/", GetProcessorArchitecture(target), "/", GetBuildTypeSubdir(), ((options & BuildOptions.Development) != BuildOptions.None) ? "/debug" : "/release" };
        string str = string.Concat(textArray1);
        string[] collection = new string[] { "WinRTBridge.winmd", "WinRTBridge.pdb", "UnityPlayer_Symbols.pdb" };
        List<string> list = new List<string>(collection);
        MetroBuildType metroBuildType = EditorUserBuildSettings.metroBuildType;
        if (metroBuildType != MetroBuildType.AppX)
        {
            if (metroBuildType != MetroBuildType.VisualStudio)
            {
                throw new ArgumentException(string.Format("Invalid metro build type {0}.", EditorUserBuildSettings.metroBuildType));
            }
        }
        else
        {
            list.Add("UnityPlayer.exe");
            goto Label_00F4;
        }
        list.Add("UnityPlayer.dll");
        list.Add("UnityPlayer.winmd");
    Label_00F4:
        foreach (string str2 in list)
        {
            string[] textArray3 = new string[] { playerPackage, "/", str, "/", str2 };
            string path = string.Concat(textArray3);
            string to = stagingArea + "/" + str2;
            if (File.Exists(path))
            {
                FileUtil.CopyFileOrDirectory(path, to);
            }
            else if (Path.GetExtension(path) != ".pdb")
            {
                throw new UnityException(string.Format("{0} file not found.", path));
            }
        }
    }

    private static void CreateDirectoriesRecursively(string root, string directory)
    {
        if (directory != string.Empty)
        {
            CreateDirectoriesRecursively(root, Path.GetDirectoryName(directory));
            CreateDirectoryIfMissing(Path.Combine(root, directory));
        }
    }

    private static void CreateDirectoryIfMissing(string dir)
    {
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
    }

    private static void CreateManifest(string path, string executable, string processorArchitecture, string storeLogo, string tileLogo, string tileWideLogo, string tileSmallLogo, string splashScreenImage, string vcLibs, bool addDependencies)
    {
        string str;
        XDocument document;
        string str3;
        XNamespace ns = "http://schemas.microsoft.com/appx/2010/manifest";
        PlayerSettings.MetroApplicationForegroundText tileForegroundText = PlayerSettings.Metro.tileForegroundText;
        if (tileForegroundText != PlayerSettings.MetroApplicationForegroundText.Light)
        {
            if (tileForegroundText != PlayerSettings.MetroApplicationForegroundText.Dark)
            {
                throw new System.Exception(string.Format("Invalid MetroApplicationForegroundText value {0}.", PlayerSettings.Metro.tileForegroundText));
            }
        }
        else
        {
            str = "light";
            goto Label_0057;
        }
        str = "dark";
    Label_0057:
        document = new XDocument(new XDeclaration("1.0", "utf-8", null), new object[0]);
        object[] content = new object[] { new XAttribute("Name", PlayerSettings.Metro.packageName), new XAttribute("Publisher", "CN=" + PlayerSettings.Metro.certificateSubject), new XAttribute("Version", PlayerSettings.Metro.packageVersion), new XAttribute("ProcessorArchitecture", processorArchitecture) };
        XElement element = new XElement((XName) (ns + "Package"), new XElement((XName) (ns + "Identity"), content));
        object[] objArray2 = new object[] { new XElement((XName) (ns + "DisplayName"), PlayerSettings.productName), new XElement((XName) (ns + "PublisherDisplayName"), PlayerSettings.companyName), new XElement((XName) (ns + "Logo"), storeLogo) };
        element.Add(new XElement((XName) (ns + "Properties"), objArray2));
        object[] objArray3 = new object[] { new XElement((XName) (ns + "OSMinVersion"), "6.2"), new XElement((XName) (ns + "OSMaxVersionTested"), "6.2") };
        element.Add(new XElement((XName) (ns + "Prerequisites"), objArray3));
        element.Add(new XElement((XName) (ns + "Resources"), new XElement((XName) (ns + "Resource"), new XAttribute("Language", "en-US"))));
        object[] objArray4 = new object[4];
        objArray4[0] = new XAttribute("Id", "App");
        objArray4[1] = new XAttribute("Executable", executable);
        objArray4[2] = new XAttribute("EntryPoint", PlayerSettings.Metro.packageName + '.' + "App");
        object[] objArray5 = new object[] { new XAttribute("DisplayName", PlayerSettings.productName), new XAttribute("Logo", tileLogo), new XAttribute("SmallLogo", tileSmallLogo), new XAttribute("Description", PlayerSettings.Metro.applicationDescription), new XAttribute("ForegroundText", str), new XAttribute("BackgroundColor", GetString(PlayerSettings.Metro.tileBackgroundColor)), new XElement((XName) (ns + "SplashScreen"), new XAttribute("Image", splashScreenImage)) };
        objArray4[3] = new XElement((XName) (ns + "VisualElements"), objArray5);
        element.Add(new XElement((XName) (ns + "Applications"), new XElement((XName) (ns + "Application"), objArray4)));
        if (EditorUserBuildSettings.metroBuildType == MetroBuildType.AppX)
        {
            if (addDependencies)
            {
                object[] objArray6 = new object[] { new XAttribute("Name", vcLibs), new XAttribute("MinVersion", new Version(11, 0, 0xc622, 1)) };
                element.Add(new XElement((XName) (ns + "Dependencies"), new XElement((XName) (ns + "PackageDependency"), objArray6)));
            }
            object[] objArray7 = new object[2];
            objArray7[0] = new XAttribute("Category", "windows.activatableClass.inProcessServer");
            object[] objArray8 = new object[0x10];
            objArray8[0] = new XElement((XName) (ns + "Path"), "CLRHost.dll");
            object[] objArray9 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.InternalCalls"), new XAttribute("ThreadingModel", "both") };
            objArray8[1] = new XElement((XName) (ns + "ActivatableClass"), objArray9);
            object[] objArray10 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.ExceptionHandling"), new XAttribute("ThreadingModel", "both") };
            objArray8[2] = new XElement((XName) (ns + "ActivatableClass"), objArray10);
            object[] objArray11 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.GCHandles"), new XAttribute("ThreadingModel", "both") };
            objArray8[3] = new XElement((XName) (ns + "ActivatableClass"), objArray11);
            object[] objArray12 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.Marshalling"), new XAttribute("ThreadingModel", "both") };
            objArray8[4] = new XElement((XName) (ns + "ActivatableClass"), objArray12);
            object[] objArray13 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.MethodTools"), new XAttribute("ThreadingModel", "both") };
            objArray8[5] = new XElement((XName) (ns + "ActivatableClass"), objArray13);
            object[] objArray14 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.MonoBehaviourSerialization"), new XAttribute("ThreadingModel", "both") };
            objArray8[6] = new XElement((XName) (ns + "ActivatableClass"), objArray14);
            object[] objArray15 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.ObjectInstantiation"), new XAttribute("ThreadingModel", "both") };
            objArray8[7] = new XElement((XName) (ns + "ActivatableClass"), objArray15);
            object[] objArray16 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.ScriptingClassWrapper"), new XAttribute("ThreadingModel", "both") };
            objArray8[8] = new XElement((XName) (ns + "ActivatableClass"), objArray16);
            object[] objArray17 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.ScriptingMethodWrapper"), new XAttribute("ThreadingModel", "both") };
            objArray8[9] = new XElement((XName) (ns + "ActivatableClass"), objArray17);
            object[] objArray18 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.ScriptingPinnedArray"), new XAttribute("ThreadingModel", "both") };
            objArray8[10] = new XElement((XName) (ns + "ActivatableClass"), objArray18);
            object[] objArray19 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.SerializationReader"), new XAttribute("ThreadingModel", "both") };
            objArray8[11] = new XElement((XName) (ns + "ActivatableClass"), objArray19);
            object[] objArray20 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.SerializationWriter"), new XAttribute("ThreadingModel", "both") };
            objArray8[12] = new XElement((XName) (ns + "ActivatableClass"), objArray20);
            object[] objArray21 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.StringTools"), new XAttribute("ThreadingModel", "both") };
            objArray8[13] = new XElement((XName) (ns + "ActivatableClass"), objArray21);
            object[] objArray22 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.TypeInformation"), new XAttribute("ThreadingModel", "both") };
            objArray8[14] = new XElement((XName) (ns + "ActivatableClass"), objArray22);
            object[] objArray23 = new object[] { new XAttribute("ActivatableClassId", "WinRTBridge.UnityEngineObjectTools"), new XAttribute("ThreadingModel", "both") };
            objArray8[15] = new XElement((XName) (ns + "ActivatableClass"), objArray23);
            objArray7[1] = new XElement((XName) (ns + "InProcessServer"), objArray8);
            element.Add(new XElement((XName) (ns + "Extensions"), new XElement((XName) (ns + "Extension"), objArray7)));
        }
        document.Add(element);
        XElement visualElementsElement = document.Element((XName) (ns + "Package")).Element((XName) (ns + "Applications")).Element((XName) (ns + "Application")).Element((XName) (ns + "VisualElements"));
        string tileShortName = PlayerSettings.Metro.tileShortName;
        if (!string.IsNullOrEmpty(tileShortName))
        {
            GetDefaultTileElement(ns, visualElementsElement).Add(new XAttribute("ShortName", tileShortName));
        }
        switch (PlayerSettings.Metro.tileShowName)
        {
            case PlayerSettings.MetroApplicationShowName.NotSet:
                str3 = null;
                break;

            case PlayerSettings.MetroApplicationShowName.AllLogos:
                str3 = "allLogos";
                break;

            case PlayerSettings.MetroApplicationShowName.NoLogos:
                str3 = "noLogos";
                break;

            case PlayerSettings.MetroApplicationShowName.StandardLogoOnly:
                str3 = "logoOnly";
                break;

            case PlayerSettings.MetroApplicationShowName.WideLogoOnly:
                str3 = "wideLogoOnly";
                break;

            default:
                throw new System.Exception(string.Format("Invalid MetroApplicationShowName value {0}.", PlayerSettings.Metro.tileShowName));
        }
        if (!string.IsNullOrEmpty(str3))
        {
            GetDefaultTileElement(ns, visualElementsElement).Add(new XAttribute("ShowName", str3));
        }
        if (!string.IsNullOrEmpty(tileWideLogo))
        {
            GetDefaultTileElement(ns, visualElementsElement).Add(new XAttribute("WideLogo", tileWideLogo));
        }
        Color? splashScreenBackgroundColor = PlayerSettings.Metro.splashScreenBackgroundColor;
        if (splashScreenBackgroundColor.HasValue)
        {
            visualElementsElement.Element((XName) (ns + "SplashScreen")).Add(new XAttribute("BackgroundColor", GetString(splashScreenBackgroundColor.Value)));
        }
        document.Save(path);
    }

    private static void CreateVisualStudioSolution(string installPath, string stagingArea, string playerPackage, bool sourceBuild)
    {
        FileUtil.CopyFileOrDirectory(Path.Combine(playerPackage, "Tools/UnityTest.pfx"), stagingArea + "/UnityTest.pfx");
        if (!sourceBuild)
        {
            ReplaceDirectoyEx(stagingArea + "/Players", installPath + "/Players");
        }
        FileUtil.CopyFileOrDirectory(Path.Combine(playerPackage, "Template/Common"), stagingArea + "/Common");
        foreach (string str2 in Directory.GetFiles(Path.Combine(playerPackage, "Template")))
        {
            FileUtil.CopyFileOrDirectory(str2, stagingArea + "/" + Path.GetFileName(str2));
        }
        MetroVisualStudioSolutionCreator.CreateSolutionFileFrom(PlayerSettings.productName, stagingArea, installPath, dontOverwriteFiles, sourceBuild);
        FileUtil.DeleteFileOrDirectory(Path.Combine(installPath, PlayerSettings.productName + ".v11.suo"));
    }

    private static string GetBuildTypeSubdir()
    {
        MetroBuildType metroBuildType = EditorUserBuildSettings.metroBuildType;
        if (metroBuildType != MetroBuildType.AppX)
        {
            if (metroBuildType != MetroBuildType.VisualStudio)
            {
                throw new ArgumentException(string.Format("Invalid metro build type {0}.", EditorUserBuildSettings.metroBuildType));
            }
            return "lib";
        }
        return "exe";
    }

    private static XElement GetDefaultTileElement(XNamespace ns, XElement visualElementsElement)
    {
        XElement content = visualElementsElement.Element((XName) (ns + "DefaultTile"));
        if (content == null)
        {
            content = new XElement((XName) (ns + "DefaultTile"));
            visualElementsElement.AddFirst(content);
        }
        return content;
    }

    private static string GetProcessorArchitecture(BuildTarget target)
    {
        switch (target)
        {
            case BuildTarget.MetroPlayerX86:
                return "x86";

            case BuildTarget.MetroPlayerX64:
                return "x64";

            case BuildTarget.MetroPlayerARM:
                return "arm";
        }
        throw new ArgumentException(string.Format("Invalid build target {0}.", target), "target");
    }

    private static string GetString(Color color)
    {
        Color32 color2 = color;
        return string.Format("#{0:X2}{1:X2}{2:X2}", color2.r, color2.g, color2.b);
    }

    private static void InstallPluginsMetro(string targetDirectory)
    {
        string path = @"Assets\Plugins";
        if (Directory.Exists(path))
        {
            CopyAssemblies(path, targetDirectory, "dll");
            CopyAssemblies(path, targetDirectory, "winmd");
        }
    }

    public static void PostProcess(BuildTarget target, BuildOptions options, string installPath, string stagingAreaData, string stagingArea, string playerPackage)
    {
        string str25;
        if (target == BuildTarget.MetroPlayerX64)
        {
            throw new System.Exception("Only MetroPlayerX86, MetroPlayerARM are supported");
        }
        FileUtil.CreateOrCleanDirectory(CombinePath(stagingArea, "Assets"));
        string storeLogo = ProcessImage(PlayerSettings.Metro.packageLogo, CombinePath(playerPackage, "StoreLogo.png"), stagingArea);
        string tileLogo = ProcessImage(PlayerSettings.Metro.tileLogo, CombinePath(playerPackage, "SquareTile.png"), stagingArea);
        string tileWideLogo = ProcessImage(PlayerSettings.Metro.tileWideLogo, null, stagingArea);
        string tileSmallLogo = ProcessImage(PlayerSettings.Metro.tileSmallLogo, CombinePath(playerPackage, "SmallTile.png"), stagingArea);
        string splashScreenImage = ProcessImage(PlayerSettings.Metro.splashScreenImage, CombinePath(playerPackage, "SplashScreen.png"), stagingArea);
        string[] strArray = new string[] { "Data/Resources/unity default resources", "Managed/UnityEngine.dll", "Managed/UnityEngine.pdb" };
        string executable = (EditorUserBuildSettings.metroBuildType != MetroBuildType.AppX) ? (PlayerSettings.productName + ".exe") : "UnityPlayer.exe";
        FileUtil.CreateOrCleanDirectory(Path.Combine(stagingArea, "Data/Resources"));
        FileUtil.CreateOrCleanDirectory(Path.Combine(stagingArea, "Managed"));
        foreach (string str7 in strArray)
        {
            FileUtil.CopyFileOrDirectory(playerPackage + "/" + str7, stagingArea + "/" + str7);
        }
        FileUtil.CopyFileOrDirectory(playerPackage + "/Players/ARM/d3dcompiler_45.dll", stagingArea + "/d3dcompiler_45.dll");
        string vcLibs = "Microsoft.VCLibs.110.00";
        if ((options & BuildOptions.Development) != BuildOptions.None)
        {
            vcLibs = vcLibs + ".Debug";
        }
        if (string.IsNullOrEmpty(PlayerSettings.Metro.certificatePath))
        {
            throw new UnityException("Certificate has not been set in Metro publishing settings.");
        }
        CreateManifest(stagingArea + "/AppxManifest.xml", executable, GetProcessorArchitecture(target), storeLogo, tileLogo, tileWideLogo, tileSmallLogo, splashScreenImage, vcLibs, (EditorUserBuildSettings.metroBuildType == MetroBuildType.AppX) && (target == BuildTarget.MetroPlayerARM));
        List<string> list = new List<string>(Directory.GetFiles(Path.Combine(stagingArea, "Data/Managed"), "*.dll")) {
            Path.Combine(stagingArea, "Managed/UnityEngine.dll")
        };
        string fileName = Path.Combine(playerPackage, "Tools/AssemblyPreprocessor.exe");
        foreach (string str10 in list)
        {
            string str11 = Path.GetFileName(str10);
            if ((File.Exists(str10) && (str11.IndexOf("Boo.") != 0)) && (str11.IndexOf("Mono.") != 0))
            {
                string str12;
                string arguments = string.Format("\"{0}\" -injectCtor -assemblyPath \"{1}\" -pdb", str10, Path.Combine(stagingArea, "Managed"));
                if (RunAndWait(fileName, arguments, out str12) != 0)
                {
                    throw new System.Exception(string.Format("Failed to run assembly preprocessor with cmdline {2}.[{0}]\n{1}", str10, str12, arguments));
                }
            }
        }
        string path = Path.Combine(stagingArea, "Data/Managed");
        string source = Path.Combine(stagingArea, "Managed");
        if (Directory.Exists(path))
        {
            FileUtil.CopyDirectoryRecursive(path, stagingArea);
            Directory.Delete(path, true);
        }
        FileUtil.CopyDirectoryRecursive(source, stagingArea);
        Directory.Delete(source, true);
        if (Directory.Exists("Assets/StreamingAssets"))
        {
            FileUtil.CopyDirectoryRecursiveForPostprocess("Assets/StreamingAssets", stagingArea + "/Data/StreamingAssets", true);
        }
        InstallPluginsMetro(stagingArea);
        if ((options & BuildOptions.InstallInBuildFolder) == BuildOptions.None)
        {
            MetroBuildType metroBuildType = EditorUserBuildSettings.metroBuildType;
            if (metroBuildType != MetroBuildType.AppX)
            {
                if (metroBuildType != MetroBuildType.VisualStudio)
                {
                    throw new System.Exception("Unknown metro build type:" + EditorUserBuildSettings.metroBuildType.ToString());
                }
            }
            else
            {
                string str19;
                CopyPlayerFiles(target, options, stagingArea, playerPackage);
                string str16 = FileUtil.NiceWinPath(installPath);
                string str17 = Path.Combine(playerPackage, @"Tools\MetroUtility.exe");
                string str18 = "createPackage /silent";
                str25 = str18;
                object[] objArray1 = new object[] { str25, " /manifest \"", FileUtil.NiceWinPath(CombinePath(stagingArea, "AppxManifest.xml")), '"' };
                str25 = string.Concat(objArray1);
                object[] objArray2 = new object[] { str25, " /package \"", str16, '"' };
                str18 = string.Concat(objArray2);
                int num3 = RunAndWait(str17, str18, out str19);
                if (num3 == 0)
                {
                    str18 = "signPackage /silent";
                    str25 = str18;
                    object[] objArray3 = new object[] { str25, " /package \"", str16, '"' };
                    str18 = string.Concat(objArray3);
                    string fullPath = FileUtil.NiceWinPath(PlayerSettings.Metro.certificatePath);
                    if (!Path.IsPathRooted(fullPath))
                    {
                        fullPath = Path.GetFullPath(fullPath);
                    }
                    str25 = str18;
                    object[] objArray4 = new object[] { str25, " /certificate \"", fullPath, '"' };
                    str18 = string.Concat(objArray4);
                    if (!string.IsNullOrEmpty(PlayerSettings.Metro.certificatePassword))
                    {
                        str18 = str18 + " /password \"" + PlayerSettings.Metro.certificatePassword + "\"";
                    }
                    num3 = RunAndWait(str17, str18, out str19);
                    if (num3 != 0)
                    {
                        try
                        {
                            File.Delete(str16);
                        }
                        catch
                        {
                        }
                        if (string.IsNullOrEmpty(str19))
                        {
                            throw new System.Exception(string.Format("Failed to sign package (0x{0:x8}).", num3));
                        }
                        throw new System.Exception(string.Format("Failed to sign package (0x{0:x8}).\r\n{1}", num3, str19));
                    }
                    return;
                }
                if (string.IsNullOrEmpty(str19))
                {
                    throw new System.Exception(string.Format("Failed to create package (0x{0:x8}).", num3));
                }
                throw new System.Exception(string.Format("Failed to create package (0x{0:x8}).\r\n{1}", num3, str19));
            }
            CopyPlayerFiles(target, options, stagingArea, playerPackage);
            CreateDirectoryIfMissing(installPath);
            CreateVisualStudioSolution(installPath, stagingArea, playerPackage, false);
        }
        else
        {
            string str21 = Path.Combine(playerPackage, "SourceBuild");
            if (Directory.Exists(str21) && (EditorUserBuildSettings.metroBuildType == MetroBuildType.AppX))
            {
                Directory.Delete(str21, true);
            }
            Thread.Sleep(100);
            CreateDirectoryIfMissing(str21);
            Thread.Sleep(100);
            switch (EditorUserBuildSettings.metroBuildType)
            {
                case MetroBuildType.AppX:
                {
                    string str24;
                    CopyPlayerFiles(target, options, stagingArea, playerPackage);
                    FileUtil.CopyDirectoryRecursive(stagingArea, str21);
                    string str22 = Path.Combine(playerPackage, @"Tools\MetroUtility.exe");
                    string str23 = "registerDevelopmentPackage --silent";
                    str25 = str23;
                    object[] objArray5 = new object[] { str25, " --manifest \"", FileUtil.NiceWinPath(CombinePath(str21, "AppxManifest.xml")), '"' };
                    str23 = string.Concat(objArray5);
                    int num4 = RunAndWait(str22, str23, out str24);
                    if (num4 != 0)
                    {
                        if (string.IsNullOrEmpty(str24))
                        {
                            throw new System.Exception(string.Format("Failed to register development package (0x{0:x8}).", num4));
                        }
                        throw new System.Exception(string.Format("Failed to register development package (0x{0:x8}).\r\n{1}", num4, str24));
                    }
                    break;
                }
                case MetroBuildType.VisualStudio:
                    CreateVisualStudioSolution(str21, stagingArea, playerPackage, true);
                    break;
            }
        }
    }

    private static string ProcessImage(string source, string defaultSource, string stagingArea)
    {
        if (string.IsNullOrEmpty(source))
        {
            if (string.IsNullOrEmpty(defaultSource))
            {
                return null;
            }
            source = defaultSource;
        }
        string str = CombinePath("Assets", Path.GetFileName((defaultSource != null) ? defaultSource : source));
        string to = CombinePath(stagingArea, str);
        FileUtil.CopyFileOrDirectory(source, to);
        return FileUtil.NiceWinPath(str);
    }

    private static void ReplaceDirectoyEx(string srcDirectory, string dstDirectory)
    {
        CreateDirectoryIfMissing(dstDirectory);
        List<string> list = new List<string>();
        list.AddRange(Directory.GetFiles(srcDirectory, "*.dll", SearchOption.AllDirectories));
        list.AddRange(Directory.GetFiles(srcDirectory, "*.winmd", SearchOption.AllDirectories));
        list.AddRange(Directory.GetFiles(srcDirectory, "*.pdb", SearchOption.AllDirectories));
        foreach (string str in list)
        {
            string path = str.Substring(srcDirectory.Length + 1);
            if (Path.GetFileName(path) != "d3dcompiler_45.dll")
            {
                CreateDirectoriesRecursively(dstDirectory, Path.GetDirectoryName(path));
                string fileName = Path.Combine(srcDirectory, path);
                string str5 = Path.Combine(dstDirectory, path);
                if (File.Exists(str5))
                {
                    FileInfo info = new FileInfo(fileName);
                    FileInfo info2 = new FileInfo(str5);
                    if ((info.Length == info2.Length) && (info.LastWriteTime == info2.LastWriteTime))
                    {
                        continue;
                    }
                }
                File.Copy(fileName, str5, true);
            }
        }
    }

    private static int RunAndWait(string fileName, string arguments, out string result)
    {
        Process process = new Process {
            StartInfo = { FileName = fileName, Arguments = arguments, CreateNoWindow = true, UseShellExecute = false, RedirectStandardOutput = true }
        };
        process.Start();
        process.WaitForExit();
        result = process.StandardOutput.ReadToEnd();
        return process.ExitCode;
    }
}

